
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "inf2csv.h"

#include "csvline.h"
#include "MainFrm.h"
#include "PaneView.h"
#include "BodyView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

const DWORD MENU_STYLES[]	= { AFX_MBV_KEEPVISIBLE, AFX_MBV_DISPLAYONFOCUS, AFX_MBV_KEEPVISIBLE };
WCHAR	 VIEW_PROFILE[] = L"11111\0";
unsigned VEW_CONTROLS[6]	= { ID_VIEW_FIXED_MENU, 
	ID_VIEW_TOOLBAR, 
	ID_VIEW_PANE_HEAD, 
	ID_VIEW_PANE_FOOTER, 
	ID_VIEW_STATUS_BAR, 0 };
void id2pos(unsigned apos, BOOL avalue)
{
	for (int i(1); (VEW_CONTROLS[i]) && (VIEW_PROFILE[i]); ++i)
		if (apos == VEW_CONTROLS[i])
		{
			VIEW_PROFILE[i] = (avalue) ? '1' : '0';
			break;
		}
}

static const POINT pointMin = { 333, 222 };	//	stdafx.cpp
bool restorePos(CWinApp * pApp, const POINT & aminiPoint, CREATESTRUCT& cs);

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_MESSAGE_VOID(WM_INITIALUPDATE, CMainFrame::OnInitdialog)
	ON_WM_GETMINMAXINFO()
	ON_COMMAND_EX(ID_VIEW_STATUS_BAR, &CMainFrame::OnBarCheck)
	ON_COMMAND_EX(ID_VIEW_TOOLBAR, &CMainFrame::OnBarCheck)
	ON_COMMAND_RANGE(ID_VIEW_PANE_HEAD, ID_VIEW_PANE_FOOTER, CMainFrame::OnViewPane)
	ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_PANE_HEAD, ID_VIEW_PANE_FOOTER, CMainFrame::OnUpdateViewPane)
	ON_UPDATE_COMMAND_UI(ID_VIEW_LIST, CMainFrame::OnUpdateViewStyles)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_SCRL, CMainFrame::OnUpdateIndicatorReg)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_FOCUS, CMainFrame::OnUpdateIndicatorFocus)
	ON_WM_CONTEXTMENU()
	ON_WM_CLOSE()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_FOCUS,
	ID_INDICATOR_SCRL,	
};

// CMainFrame construction/destruction

CMainFrame::CMainFrame() : 
	m_bRestore(false), m_bFileSec(TRUE), 
	m_pViewList(NULL), m_pPropList(NULL) {}

//	START
// Implementation of m_wndPropList
BOOL CMainFrame::toggleDescArea()
{
	BOOL bShow = m_pPropList->IsDescriptionArea();
		bShow = (bShow) ? FALSE : TRUE;
	m_pPropList->EnableDescriptionArea(bShow);
	return bShow;
}

BOOL CMainFrame::toggleFileSect()
{
	CMFCPropertyGridProperty * pPro = m_pPropList->GetProperty(0);
	BOOL newval = (pPro->IsVisible()) ? FALSE : TRUE;
	pPro->Show(newval);

	if (!newval)
	if (m_pPropList->GetPropertyCount() >> 1)
	{
		pPro = m_pPropList->GetCurSel();
		if (pPro)
			if (!(pPro->IsVisible()))
				m_pPropList->SetCurSel(m_pPropList->GetProperty(1));
	}
	else { 
		m_pPropList->SetCurSel(NULL);
		m_wndSplitter.ActivateNext();
	}

	return newval;
}
// Implementation of m_wndPropList
//	FINISH

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	//	restore settings of view sub menu
	CString thestr = theApp.GetProfileString(L"window", L"view",  L"11111");
	if (thestr.GetLength() < 6) ::wcsncpy(VIEW_PROFILE, thestr, 5);

	//	TODO: show\hide menu
	DWORD thestyle = AFX_MBV_KEEPVISIBLE;
	if ((* VIEW_PROFILE) == '2') thestyle = AFX_MBV_DISPLAYONFOCUS;
	SetMenuBarVisibility(thestyle);

	for (int i(1); (VEW_CONTROLS[i]) && (VIEW_PROFILE[i]); ++i)
			if (VIEW_PROFILE[i] < L'1') 
				PostMessage(WM_COMMAND, VEW_CONTROLS[i]);
	//	restore settings of view sub menu

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);

/*	if (!m_wndDlgBar.Create(this, IDR_MAINFRAME, CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR))
	{
		TRACE0("Failed to create dialogbar\n");
		return -1;		// fail to create
	}	*/
	//W_CONTROLS[2] = 0;	//	(unsigned) m_wndDlgBar.m_hWnd;

	if (!m_wndReBar.Create(this) || !m_wndReBar.AddBar(&m_wndToolBar))
	{	// || !m_wndReBar.AddBar(&m_wndDlgBar))
		TRACE0("Failed to create rebar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.CreateEx(this, SBARS_SIZEGRIP | SBT_TOOLTIPS))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));
	
	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	CBodyView::g_nMode = theApp.GetProfileInt(L"window", L"mode", 0);

	UINT thewidth = CPaneView::m_colwidth * 2;
		thewidth = theApp.GetProfileIntW(L"window", L"pane", thewidth);
	if (m_bRestore) thewidth = CPaneView::getWidth(this, thewidth);

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CPaneView), CSize(thewidth, 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CBodyView), CSize(100, 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

	CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
	CPaneView * pPane = DYNAMIC_DOWNCAST(CPaneView, pWnd);
		m_pPropList = pPane->m_pPropList;

	m_pViewList = (CListView *) m_wndSplitter.GetPane(0, 1);

	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
	m_bRestore = restorePos(& theApp, pointMin, cs);
	
	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}
#endif //_DEBUG

// CMainFrame attributes


// CMainFrame message handlers

void CMainFrame::OnUpdateViewStyles(CCmdUI* pCmdUI)
{
	// if the right-hand pane hasn't been created or isn't a view, disable commands in our range
	if (!pCmdUI || !m_pViewList) return;

	DWORD dwStyle = m_pViewList->GetStyle() & LVS_TYPEMASK;
	if (ID_VIEW_LIST == pCmdUI->m_nID)
	{
		//	bChecked = (dwStyle == LVS_LIST);
		pCmdUI->Enable(dwStyle != LVS_SMALLICON);
		pCmdUI->SetRadio(CBodyView::isRegMode());
	}
		else pCmdUI->Enable();

	return;
}

void CMainFrame::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	WINDOWPLACEMENT wp = { sizeof(WINDOWPLACEMENT), 0, 0 };
		LPRECT pRect = & (wp.rcNormalPosition);
		::memset(pRect, 0, sizeof(RECT));
		
	if(::GetWindowPlacement(m_hWnd, & wp))
		if(!(::IsRectEmpty(pRect)))
			theApp.WriteProfileBinary(  L"window", 
										L"pos", 
									(LPBYTE) pRect, 
										sizeof(RECT) );

	theApp.WriteProfileString(L"window", L"view", VIEW_PROFILE);

	RECT therect;
		::memset(& therect, 0, sizeof(RECT));
	if (m_pPropList) m_pPropList->GetClientRect(& therect);
	if (therect.right > 0) theApp.WriteProfileInt(L"window", L"pane", therect.right);

	theApp.WriteProfileInt(L"window", L"mode", (CBodyView::g_nMode & 1));

	CFrameWnd::OnClose();
}

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	// TODO: Add your message handler code here and/or call default
	lpMMI->ptMinTrackSize = pointMin;
	//MMI->ptMinTrackSize.y = 222;
	CFrameWnd::OnGetMinMaxInfo(lpMMI);
}

void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint apoint)
{
	if (cmn::CommonTrackMenu(this, apoint, CBodyView::VIEW_MENU)) return;
	PostMessage(WM_COMMAND, ID_VIEW_FIXED_MENU);
}

//	called once
	//T CMainFrame::OnInitdialog(WPARAM wParam, LPARAM lParam)
void CMainFrame::OnInitdialog()
{
//	if (theApp.m_pCmdInfo) theApp.OnOpenRecentFile(0);
	if (theApp.m_pCmdInfo) 
		theApp.OpenDocumentFile((theApp.m_pCmdInfo)->m_strFileName);

//		m_wndStatusBar.SetPaneStyle(index, SBPS_NORMAL);	//SBPS_POPOUT);
	int index = m_wndStatusBar.CommandToIndex(ID_INDICATOR_FOCUS);
	if (index + 1) //	?
	{
		m_wndStatusBar.SetPaneText(index, L"");
		CString thestr;
			thestr.LoadString(IDS_TOOLTIP_FOCUS);
		(m_wndStatusBar.GetStatusBarCtrl()).SetTipText(index, thestr);
	}
}

void CMainFrame::OnUpdateIndicatorReg(CCmdUI *pCmdUI)
{
	if (!m_pViewList) return;
	CListCtrl * pControl = & (m_pViewList->GetListCtrl());
	if (pControl) 
	if (CBodyView::isReportStyle(pControl)) 
	{
		pCmdUI->Enable(CBodyView::isRegMode());
		return;
	}

	pCmdUI->Enable(FALSE);
	return;
}

void CMainFrame::OnUpdateIndicatorFocus(CCmdUI *pCmdUI)
{
	CWnd * pWnd = m_wndSplitter.GetActivePane();
	if (!pWnd || !pCmdUI) return;
	CString thestr = L"\x20\x20\x20\x20\x20\x20\x20\x20";
		pWnd->GetWindowText(thestr);
		thestr.Append(L"\x20\x20\x20\x20");
	pCmdUI->SetText(thestr);

	return;
}

void CMainFrame::OnUpdateViewPane(CCmdUI *pCmdUI)
{
	BOOL bShow;
//		if(pCmdUI->m_nID ^ ID_EDIT_SELECT_ALL)
	if (pCmdUI)
	switch (pCmdUI->m_nID)
	{
	case ID_VIEW_PANE_HEAD : 
		bShow = (m_pPropList->GetProperty(0))->IsVisible();
			pCmdUI->SetCheck(bShow);
		break;

	case ID_VIEW_PANE_FOOTER : 
		pCmdUI->SetCheck(m_pPropList->IsDescriptionArea());
		break;

	default:	// ID_VIEW_FIXED_MENU
		DWORD dwShow = GetMenuBarVisibility();
		pCmdUI->SetCheck(dwShow == AFX_MBV_KEEPVISIBLE);
	}
}

BOOL CMainFrame::OnBarCheck(UINT nID)
{
	CControlBar* pBar = GetControlBar(nID);
	if (pBar != NULL)
	{
		BOOL bShow = ((pBar->GetStyle() & WS_VISIBLE) == 0);
		ShowControlBar(pBar, bShow, FALSE);
		id2pos(nID, bShow);
		return TRUE;
	}
	return FALSE;
}

void CMainFrame::OnViewPane(UINT nCommandID)
{
//	if (nCommandID ^ ID_VIEW_FIXED_MENU)
/*	BOOL bShow = ((m_wndDlgBar.GetStyle() & WS_VISIBLE) == 0);
	ShowControlBar(& m_wndDlgBar, bShow, FALSE);	*/
	switch (nCommandID)
	{
	case ID_VIEW_PANE_HEAD : 
			m_bFileSec = toggleFileSect();
			id2pos(nCommandID, m_bFileSec);
		break;

	case ID_VIEW_PANE_FOOTER : 
		{
			BOOL bShow = toggleDescArea();
			id2pos(nCommandID, bShow);
		}
		break;

	default:	// ID_VIEW_FIXED_MENU
		DWORD dwShow = (GetMenuBarVisibility() & AFX_MBV_KEEPVISIBLE);
		SetMenuBarVisibility(MENU_STYLES[dwShow]);
		* VIEW_PROFILE = (MENU_STYLES[dwShow] == AFX_MBV_DISPLAYONFOCUS) ? '2' : '1';
	}
}

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class
	if (LOWORD(wParam) ^ ID_VIEW_LIST) 
		return CFrameWnd::OnCommand(wParam, lParam);
	//	lite like DeleteContents()
	GetActiveDocument()->SaveModified();
	m_pViewList->SendMessageW(WM_COMMAND, wParam, lParam);
	return TRUE;
}
